home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 59 / 59.xpi / chrome / useragentswitcher.jar / content / useragentswitcher / options / options.js < prev    next >
Text File  |  2009-06-30  |  28KB  |  858 lines

  1. // User Agent Switcher options
  2. var UserAgentSwitcherOptions =
  3. {
  4.     appCodeName: null,
  5.     appName:     null,
  6.     appVersion:  null,
  7.     clipboard:   [],
  8.     description: null,
  9.     folder:      null,
  10.     platform:    null,
  11.     userAgent:   null,
  12.     vendor:      null,
  13.     vendorSub:   null,
  14.     
  15.     // Adds a tree item to the selection
  16.     addTreeItemToSelection: function(treeItem)
  17.     {
  18.         var endIndex   = {};
  19.         var startIndex = {};
  20.         var treeView   = document.getElementById("useragentswitcher-options-tree").view;
  21.         var selections = treeView.selection.getRangeCount();
  22.         var selectedItem = null;
  23.                 
  24.         // If there are no selections
  25.         if(selections == 0)
  26.         {
  27.             document.getElementById("useragentswitcher-options-user-agents").appendChild(treeItem);
  28.         }
  29.         else
  30.         {
  31.             // If there is one selection
  32.             if(selections == 1)
  33.             {
  34.                 treeView.selection.getRangeAt(0, startIndex, endIndex);
  35.             }
  36.             else
  37.             {
  38.                 treeView.selection.getRangeAt(selections - 1, startIndex, endIndex);
  39.             }
  40.                 
  41.             selectedItem = treeView.getItemAtIndex(endIndex.value);
  42.  
  43.             // If the selected item is set
  44.             if(selectedItem)
  45.             {
  46.                 // If the selected item is a folder
  47.                 if(selectedItem.hasAttribute("container"))
  48.                 {
  49.                     selectedItem.firstChild.nextSibling.appendChild(treeItem);
  50.                 }
  51.                 else
  52.                 {
  53.                     selectedItem.parentNode.insertBefore(treeItem, selectedItem);
  54.                 }
  55.             }
  56.         }
  57.     },
  58.     
  59.     // Copies user agents
  60.     copy: function()
  61.     {
  62.         var endIndex   = {};
  63.         var startIndex = {};
  64.         var treeView   = document.getElementById("useragentswitcher-options-tree").view;
  65.         var selections = treeView.selection.getRangeCount();
  66.         
  67.         this.clipboard = [];
  68.  
  69.         // If there is 1 selection
  70.         if(selections == 1)
  71.         {        
  72.             treeView.selection.getRangeAt(0, startIndex, endIndex);
  73.             
  74.             // If more than one item is selected
  75.             if(endIndex.value - startIndex.value > 0)
  76.             {
  77.                 this.copySelections(treeView, startIndex, endIndex);
  78.             }
  79.             else
  80.             {
  81.                 var selectedItem = treeView.getItemAtIndex(startIndex.value);
  82.             
  83.                 // If an item is selected
  84.                 if(selectedItem)
  85.                 {
  86.                     this.clipboard.push(selectedItem.cloneNode(true));
  87.                 }
  88.             }    
  89.         }
  90.         else
  91.         {
  92.             // Loop through the selections
  93.             for(var i = 0; i < selections; i++)
  94.             {
  95.                 treeView.selection.getRangeAt(i, startIndex, endIndex);
  96.  
  97.                 this.copySelections(treeView, startIndex, endIndex);
  98.             }
  99.         }
  100.     },
  101.     
  102.     // Copies selections
  103.     copySelections: function(treeView, startIndex, endIndex)
  104.     {
  105.         var endValue   = endIndex.value;
  106.         var startValue = startIndex.value;
  107.         
  108.         // Loop through the items in reverse order to delete without changing the index
  109.         for(var index = startValue; index <= endValue; index++)
  110.         {
  111.             this.clipboard.push(treeView.getItemAtIndex(index).cloneNode(true));
  112.         }
  113.     },
  114.     
  115.     // Cuts user agents
  116.     cut: function()
  117.     {
  118.         var endIndex   = {};
  119.         var startIndex = {};
  120.         var treeView   = document.getElementById("useragentswitcher-options-tree").view;
  121.         var selections = treeView.selection.getRangeCount();
  122.         
  123.         this.copy();
  124.  
  125.         // If there is 1 selection
  126.         if(selections == 1)
  127.         {        
  128.             treeView.selection.getRangeAt(0, startIndex, endIndex);
  129.             
  130.             // If more than one item is selected
  131.             if(endIndex.value - startIndex.value > 0)
  132.             {
  133.                 this.deleteSelections(treeView, startIndex, endIndex);
  134.             }
  135.             else
  136.             {
  137.                 var selectedItem = treeView.getItemAtIndex(startIndex.value);
  138.             
  139.                 // If an item is selected
  140.                 if(selectedItem)
  141.                 {
  142.                     UserAgentSwitcherDOM.removeElement(selectedItem);
  143.                 }
  144.             }    
  145.         }
  146.         else
  147.         {
  148.             // Loop through the selections in reverse order to delete without changing the index
  149.             for(var i = selections - 1; i >= 0; i--)
  150.             {
  151.                 treeView.selection.getRangeAt(i, startIndex, endIndex);
  152.  
  153.                 this.deleteSelections(treeView, startIndex, endIndex);
  154.             }
  155.         }
  156.     },
  157.     
  158.     // Deletes user agents
  159.     deleteUserAgents: function()
  160.     {
  161.         var endIndex   = {};
  162.         var startIndex = {};
  163.         var treeView   = document.getElementById("useragentswitcher-options-tree").view;
  164.         var selections = treeView.selection.getRangeCount();
  165.         
  166.         // If there is 1 selection
  167.         if(selections == 1)
  168.         {        
  169.             treeView.selection.getRangeAt(0, startIndex, endIndex);
  170.             
  171.             // If more than one item is selected
  172.             if(endIndex.value - startIndex.value > 0)
  173.             {
  174.                 // If the multiple deletion is confirmed
  175.                 if(confirm(UserAgentSwitcherStringBundle.getString("deleteMultipleConfirmation")))
  176.                 {
  177.                     this.deleteSelections(treeView, startIndex, endIndex);
  178.                 }
  179.             }
  180.             else
  181.             {
  182.                 var selectedItem = treeView.getItemAtIndex(startIndex.value);
  183.             
  184.                 // If an item is selected and is either a separator, or a folder or user agent and the deletion is confirmed
  185.                 if(selectedItem && (this.isSeparatorSelected(selectedItem) || (selectedItem.hasAttribute("container") && confirm(UserAgentSwitcherStringBundle.getString("deleteFolderConfirmation"))) || (!selectedItem.hasAttribute("container") && confirm(UserAgentSwitcherStringBundle.getString("deleteUserAgentConfirmation")))))
  186.                 {
  187.                     UserAgentSwitcherDOM.removeElement(selectedItem);
  188.                 }        
  189.             }    
  190.         }
  191.         else
  192.         {
  193.             // If the multiple deletion is confirmed
  194.             if(confirm(UserAgentSwitcherStringBundle.getString("deleteMultipleConfirmation")))
  195.             {
  196.                 // Loop through the selections in reverse order to delete without changing the index
  197.                 for(var i = selections - 1; i >= 0; i--)
  198.                 {
  199.                     treeView.selection.getRangeAt(i, startIndex, endIndex);
  200.     
  201.                     this.deleteSelections(treeView, startIndex, endIndex);
  202.                 }
  203.             }
  204.         }
  205.     },
  206.     
  207.     // Deletes selections
  208.     deleteSelections: function(treeView, startIndex, endIndex)
  209.     {
  210.         var endValue   = endIndex.value;
  211.         var startValue = startIndex.value;
  212.         
  213.         // Loop through the items in reverse order to delete without changing the index
  214.         for(var index = endValue; index >= startValue; index--)
  215.         {
  216.             UserAgentSwitcherDOM.removeElement(treeView.getItemAtIndex(index));
  217.         }
  218.     },
  219.     
  220.     // Edit user agents
  221.     edit: function()
  222.     {
  223.         var endIndex     = {};
  224.         var selectedItem = null;
  225.         var startIndex   = {};
  226.         var treeView     = document.getElementById("useragentswitcher-options-tree").view;
  227.         
  228.         treeView.selection.getRangeAt(0, startIndex, endIndex);
  229.  
  230.         selectedItem = treeView.getItemAtIndex(startIndex.value);
  231.         
  232.         // If the selected item is set
  233.         if(selectedItem)
  234.         {
  235.             // If the selected item is a folder
  236.             if(selectedItem.hasAttribute("container"))
  237.             {
  238.                 this.editFolder(selectedItem.firstChild.firstChild);
  239.             }
  240.             else
  241.             {
  242.                 this.editUserAgent(selectedItem.firstChild.firstChild);
  243.             }
  244.         }
  245.     },
  246.     
  247.     // Edits a folder
  248.     editFolder: function(selectedItem)
  249.     {
  250.         // If an item is selected
  251.         if(selectedItem)
  252.         {
  253.             window.openDialog("chrome://useragentswitcher/content/options/dialogs/folder.xul", "useragentswitcher-folder-dialog", "centerscreen,chrome,modal,resizable", "edit", selectedItem.getAttribute("label"));
  254.     
  255.             // If the folder is set
  256.             if(this.folder)
  257.             {
  258.                 selectedItem.setAttribute("label", this.folder);
  259.             }
  260.         }
  261.     },
  262.     
  263.     // Edits a user agent
  264.     editUserAgent: function(selectedItem)
  265.     {
  266.         // If an item is selected
  267.         if(selectedItem)
  268.         {
  269.             window.openDialog("chrome://useragentswitcher/content/options/dialogs/useragent.xul", "useragentswitcher-user-agent-dialog", "centerscreen,chrome,modal,resizable", "edit", selectedItem.getAttribute("useragentswitcherappcodename"), selectedItem.getAttribute("useragentswitcherappname"), selectedItem.getAttribute("useragentswitcherappversion"), selectedItem.getAttribute("label"), selectedItem.getAttribute("useragentswitcherplatform"), selectedItem.getAttribute("useragentswitcheruseragent"), selectedItem.getAttribute("useragentswitchervendor"), selectedItem.getAttribute("useragentswitchervendorsub"));
  270.     
  271.             // If the description is set
  272.             if(this.description)
  273.             {
  274.                 selectedItem.setAttribute("label", this.description);
  275.                 selectedItem.setAttribute("useragentswitcherappcodename", this.appCodeName);
  276.                 selectedItem.setAttribute("useragentswitcherappname", this.appName);
  277.                 selectedItem.setAttribute("useragentswitcherappversion", this.appVersion);
  278.                 selectedItem.setAttribute("useragentswitcherplatform", this.platform);
  279.                 selectedItem.setAttribute("useragentswitcheruseragent", this.userAgent);
  280.                 selectedItem.setAttribute("useragentswitchervendor", this.vendor);
  281.                 selectedItem.setAttribute("useragentswitchervendorsub", this.vendorSub);
  282.             }
  283.         }
  284.     },
  285.     
  286.     // Exports user agents to a file
  287.     exportUserAgents: function()
  288.     {
  289.         var filePicker = Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker);
  290.         var result     = null;
  291.     
  292.         filePicker.defaultExtension = "xml";
  293.         filePicker.defaultString    = "useragentswitcher.xml";
  294.     
  295.         filePicker.appendFilter(UserAgentSwitcherStringBundle.getString("xmlFileDescription"), "*.xml");
  296.         filePicker.init(window, UserAgentSwitcherStringBundle.getString("exportUserAgents"), filePicker.modeSave);
  297.     
  298.         result = filePicker.show();
  299.     
  300.         // If the user selected a file
  301.         if(result == filePicker.returnOK || result == filePicker.returnReplace)
  302.         {
  303.             var errorMessage = UserAgentSwitcherExporter.export(filePicker.file);
  304.             
  305.             // If there is an error message
  306.             if(errorMessage)
  307.             {
  308.                 alert(errorMessage);
  309.             }
  310.         }
  311.     },
  312.     
  313.     // Imports user agents from a file
  314.     importUserAgents: function()
  315.     {
  316.         var filePicker = Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker);
  317.     
  318.         filePicker.appendFilter(UserAgentSwitcherStringBundle.getString("xmlFileDescription"), "*.xml");
  319.         filePicker.init(window, UserAgentSwitcherStringBundle.getString("importUserAgents"), filePicker.modeOpen);
  320.     
  321.         // If the user selected an XML file
  322.         if(filePicker.show() == filePicker.returnOK)
  323.         {
  324.             var errorMessage = UserAgentSwitcherImporter.import(UserAgentSwitcherImporter.importTypeOptions, filePicker.file, false);
  325.             
  326.             // If there is an error message
  327.             if(errorMessage)
  328.             {
  329.                 alert(errorMessage);
  330.             }
  331.         }
  332.     },
  333.     
  334.     // Initializes the options
  335.     initialize: function()
  336.     {
  337.         UserAgentSwitcherImporter.import(UserAgentSwitcherImporter.importTypeOptions, UserAgentSwitcherImporter.getUserAgentFileLocation(), false);
  338.     
  339.         // If the hide menu preference is set
  340.         if(UserAgentSwitcherPreferences.isPreferenceSet("useragentswitcher.menu.hide"))
  341.         {
  342.             document.getElementById("useragentswitcher-menu-hide").checked = UserAgentSwitcherPreferences.getBooleanPreference("useragentswitcher.menu.hide", true);
  343.         }
  344.         else
  345.         {
  346.             document.getElementById("useragentswitcher-menu-hide").checked = false;
  347.         }
  348.  
  349.         // If the import overwrite preference is set
  350.         if(UserAgentSwitcherPreferences.isPreferenceSet("useragentswitcher.import.overwrite"))
  351.         {
  352.             document.getElementById("useragentswitcher-import-overwrite").checked = UserAgentSwitcherPreferences.getBooleanPreference("useragentswitcher.import.overwrite", true);
  353.         }
  354.         else
  355.         {
  356.             document.getElementById("useragentswitcher-import-overwrite").checked = false;
  357.         }
  358.         
  359.         document.getElementById("useragentswitcher-options-user-agents").addEventListener("dblclick", UserAgentSwitcherOptions.treeDoubleClick, false);
  360.     },
  361.     
  362.     // Returns true if a separator is selected
  363.     isSeparatorSelected: function(selectedItem)
  364.     {
  365.         // If the selected item is set and is a separator
  366.         if(selectedItem && selectedItem.nodeName == "treeseparator")
  367.         {
  368.             return true;
  369.         }    
  370.         
  371.         return false;
  372.     },
  373.     
  374.     // Moves the selected item down
  375.     moveDown: function()
  376.     {
  377.         var endIndex      = {};
  378.         var selectedItem  = null;
  379.         var startIndex    = {};
  380.         var treeView      = document.getElementById("useragentswitcher-options-tree").view;
  381.         var treeSelection = treeView.selection;
  382.         
  383.         treeSelection.getRangeAt(0, startIndex, endIndex);
  384.  
  385.         selectedItem = treeView.getItemAtIndex(startIndex.value);
  386.         
  387.         // If the selected item is set
  388.         if(selectedItem)
  389.         {
  390.             UserAgentSwitcherDOM.insertAfter(selectedItem, selectedItem.nextSibling);
  391.             treeSelection.select(startIndex.value + 1);
  392.         }
  393.     },
  394.     
  395.     // Moves the selected item up
  396.     moveUp: function()
  397.     {
  398.         var endIndex      = {};
  399.         var selectedItem  = null;
  400.         var startIndex    = {};
  401.         var treeView      = document.getElementById("useragentswitcher-options-tree").view;
  402.         var treeSelection = treeView.selection;
  403.         
  404.         treeSelection.getRangeAt(0, startIndex, endIndex);
  405.  
  406.         selectedItem = treeView.getItemAtIndex(startIndex.value);
  407.         
  408.         // If the selected item is set
  409.         if(selectedItem)
  410.         {
  411.             selectedItem.parentNode.insertBefore(selectedItem, selectedItem.previousSibling);
  412.             treeSelection.select(startIndex.value - 1);
  413.         }
  414.     },
  415.     
  416.     // Adds a new folder
  417.     newFolder: function()
  418.     {
  419.         window.openDialog("chrome://useragentswitcher/content/options/dialogs/folder.xul", "useragentswitcher-folder-dialog", "centerscreen,chrome,modal,resizable", "new");
  420.     
  421.         // If the folder is set
  422.         if(this.folder)
  423.         {
  424.             var userAgents   = document.getElementById("useragentswitcher-options-user-agents");
  425.             var folderCount  = userAgents.getElementsByTagName("treechildren").length + 1;
  426.             var treeCell     = document.createElement("treecell");
  427.             var treeChildren = document.createElement("treechildren");
  428.             var treeItem     = document.createElement("treeitem");
  429.             var treeRow      = document.createElement("treerow");
  430.  
  431.             treeCell.setAttribute("label", this.folder);
  432.             treeChildren.setAttribute("id", "useragentswitcher-options-folder-" + folderCount);
  433.             treeItem.setAttribute("container", true);
  434.             treeRow.appendChild(treeCell);
  435.             treeItem.appendChild(treeRow);
  436.             treeItem.appendChild(treeChildren);
  437.             
  438.             this.addTreeItemToSelection(treeItem);    
  439.         }
  440.     },
  441.     
  442.     // Adds a new separator
  443.     newSeparator: function()
  444.     {
  445.         this.addTreeItemToSelection(document.createElement("treeseparator"));    
  446.     },
  447.     
  448.     // Adds a new user agent
  449.     newUserAgent: function()
  450.     {
  451.         window.openDialog("chrome://useragentswitcher/content/options/dialogs/useragent.xul", "useragentswitcher-user-agent-dialog", "centerscreen,chrome,modal,resizable", "new");
  452.     
  453.         // If the description is set
  454.         if(this.description)
  455.         {
  456.             var treeCell = document.createElement("treecell");
  457.             var treeItem = document.createElement("treeitem");
  458.             var treeRow  = document.createElement("treerow");
  459.     
  460.             treeCell.setAttribute("label", this.description);
  461.             treeRow.appendChild(treeCell);
  462.             treeItem.appendChild(treeRow);
  463.  
  464.             treeCell.setAttribute("useragentswitcherappcodename", this.appCodeName);
  465.             treeCell.setAttribute("useragentswitcherappname", this.appName);
  466.             treeCell.setAttribute("useragentswitcherappversion", this.appVersion);
  467.             treeCell.setAttribute("useragentswitcherplatform", this.platform);
  468.             treeCell.setAttribute("useragentswitcheruseragent", this.userAgent);
  469.             treeCell.setAttribute("useragentswitchervendor", this.vendor);
  470.             treeCell.setAttribute("useragentswitchervendorsub", this.vendorSub);
  471.             
  472.             this.addTreeItemToSelection(treeItem);    
  473.         }
  474.     },
  475.     
  476.     // Opens the user agents page
  477.     openUserAgentsPage: function()
  478.     {
  479.         var parentWindow = null;
  480.     
  481.         // If there is a parent window
  482.         if(window.opener)
  483.         {
  484.             // If there is a grand parent window and it has the extension menu
  485.             if(window.opener.opener && window.opener.opener.document.getElementById("useragentswitcher-menu"))
  486.             {
  487.                 parentWindow = window.opener.opener;
  488.             }
  489.             else
  490.             {
  491.                 parentWindow = window.opener;
  492.             }
  493.         }
  494.     
  495.         // If a parent window was found
  496.         if(parentWindow)
  497.         {
  498.             parentWindow.getBrowser().selectedTab = parentWindow.getBrowser().addTab("http://chrispederick.com/work/user-agent-switcher/user-agents/");
  499.     
  500.             window.close();
  501.         }
  502.     },
  503.     
  504.     // Pastes user agents
  505.     paste: function()
  506.     {
  507.         var clipboardLength = this.clipboard.length;
  508.         var endIndex     = {};
  509.         var selectedItem = null;
  510.         var startIndex   = {};
  511.         var treeView     = document.getElementById("useragentswitcher-options-tree").view;
  512.         var selections   = treeView.selection.getRangeCount();
  513.         
  514.         // If there are no selections
  515.         if(selections == 0)
  516.         {
  517.             var userAgents = document.getElementById("useragentswitcher-options-user-agents");
  518.         
  519.             // Loop through the clipboard
  520.             for(var i = 0; i < clipboardLength; i++)
  521.             {
  522.                 userAgents.appendChild(this.clipboard[i]);
  523.             }
  524.         }
  525.         else
  526.         {
  527.             treeView.selection.getRangeAt(0, startIndex, endIndex);
  528.     
  529.             selectedItem = treeView.getItemAtIndex(startIndex.value);
  530.             
  531.             // If the selected item is set
  532.             if(selectedItem)
  533.             {
  534.                 // If the selected item is a folder
  535.                 if(selectedItem.hasAttribute("container"))
  536.                 {
  537.                     // Loop through the clipboard
  538.                     for(var i = 0; i < clipboardLength; i++)
  539.                     {
  540.                         selectedItem.firstChild.nextSibling.appendChild(this.clipboard[i]);
  541.                     }
  542.                 }
  543.                 else
  544.                 {
  545.                     // Loop through the clipboard
  546.                     for(var i = 0; i < clipboardLength; i++)
  547.                     {
  548.                         selectedItem.parentNode.insertBefore(this.clipboard[i], selectedItem);
  549.                     }
  550.                 }
  551.             }
  552.         }
  553.     },
  554.     
  555.     // Resets the user's options
  556.     resetOptions: function()
  557.     {
  558.         var promptServiceInterface = Components.interfaces.nsIPromptService;
  559.     
  560.         // If the reset is confirmed
  561.         if(Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(promptServiceInterface).confirmEx(null, UserAgentSwitcherStringBundle.getString("resetConfirmationMessage"), UserAgentSwitcherStringBundle.getString("resetConfirmation"), promptServiceInterface.BUTTON_TITLE_IS_STRING * promptServiceInterface.BUTTON_POS_0 + promptServiceInterface.BUTTON_TITLE_CANCEL * promptServiceInterface.BUTTON_POS_1, UserAgentSwitcherStringBundle.getString("reset"), null, null, null, {}) == 0)
  562.         {
  563.             UserAgentSwitcherPreferences.deletePreferenceBranch("useragentswitcher.");
  564.             UserAgentSwitcherUpgrade.setVersion();
  565.             UserAgentSwitcherDOM.removeAllChildElements(document.getElementById("useragentswitcher-options-user-agents"));    
  566.             UserAgentSwitcherImporter.reset();
  567.     
  568.             this.initialize();
  569.         }
  570.     },
  571.     
  572.     // Saves the user's options
  573.     saveOptions: function()
  574.     {
  575.         UserAgentSwitcherPreferences.setBooleanPreference("useragentswitcher.menu.hide", document.getElementById("useragentswitcher-menu-hide").checked);
  576.         UserAgentSwitcherPreferences.setBooleanPreference("useragentswitcher.import.overwrite", document.getElementById("useragentswitcher-import-overwrite").checked);
  577.  
  578.         UserAgentSwitcherExporter.export(UserAgentSwitcherExporter.getUserAgentFileLocation());
  579.         UserAgentSwitcherImporter.import(UserAgentSwitcherImporter.importTypeMenu, UserAgentSwitcherImporter.getUserAgentFileLocation(), false);
  580.     },
  581.     
  582.     // Handles a user agent being selected
  583.     selectUserAgent: function(tree)
  584.     {
  585.         var treeView   = tree.view;
  586.         var selections = treeView.selection.getRangeCount();
  587.         
  588.         // If there are no selections
  589.         if(selections == 0)
  590.         {
  591.             document.getElementById("useragentswitcher-delete-button").setAttribute("disabled", true);
  592.             document.getElementById("useragentswitcher-edit-button").setAttribute("disabled", true);
  593.             document.getElementById("useragentswitcher-move-down-button").setAttribute("disabled", true);
  594.             document.getElementById("useragentswitcher-move-up-button").setAttribute("disabled", true);
  595.         }
  596.         else if(selections == 1)
  597.         {
  598.             var endIndex   = {};
  599.             var startIndex = {};
  600.             
  601.             treeView.selection.getRangeAt(0, startIndex, endIndex);
  602.             
  603.             // If more than one item is selected
  604.             if(endIndex.value - startIndex.value > 0)
  605.             {
  606.                 // If the delete button is disabled
  607.                 if(document.getElementById("useragentswitcher-delete-button").hasAttribute("disabled"))
  608.                 {
  609.                     document.getElementById("useragentswitcher-delete-button").removeAttribute("disabled");
  610.                 }        
  611.                 
  612.                 document.getElementById("useragentswitcher-edit-button").setAttribute("disabled", true);
  613.                 document.getElementById("useragentswitcher-move-down-button").setAttribute("disabled", true);
  614.                 document.getElementById("useragentswitcher-move-up-button").setAttribute("disabled", true);
  615.             }
  616.             else
  617.             {
  618.                 var selectedItem = treeView.getItemAtIndex(startIndex.value);
  619.             
  620.                 // If the delete button is disabled
  621.                 if(document.getElementById("useragentswitcher-delete-button").hasAttribute("disabled"))
  622.                 {
  623.                     document.getElementById("useragentswitcher-delete-button").removeAttribute("disabled");
  624.                 }        
  625.                 
  626.                 // If a separator is selected
  627.                 if(this.isSeparatorSelected(treeView.getItemAtIndex(startIndex.value)))
  628.                 {
  629.                     document.getElementById("useragentswitcher-edit-button").setAttribute("disabled", true);
  630.                 }        
  631.                 else if(document.getElementById("useragentswitcher-edit-button").hasAttribute("disabled"))
  632.                 {
  633.                     document.getElementById("useragentswitcher-edit-button").removeAttribute("disabled");
  634.                 }        
  635.                 
  636.                 // If the selected item is set and has a previous sibling
  637.                 if(selectedItem && selectedItem.previousSibling)
  638.                 {
  639.                     // If the move up menu is disabled
  640.                     if(document.getElementById("useragentswitcher-move-up-button").hasAttribute("disabled"))
  641.                     {
  642.                         document.getElementById("useragentswitcher-move-up-button").removeAttribute("disabled");
  643.                     }        
  644.                 }
  645.                 else
  646.                 {
  647.                     document.getElementById("useragentswitcher-move-up-button").setAttribute("disabled", true);
  648.                 }
  649.                 
  650.                 // If the selected item is set and has a next sibling
  651.                 if(selectedItem && selectedItem.nextSibling)
  652.                 {
  653.                     // If the move down menu is disabled
  654.                     if(document.getElementById("useragentswitcher-move-down-button").hasAttribute("disabled"))
  655.                     {
  656.                         document.getElementById("useragentswitcher-move-down-button").removeAttribute("disabled");
  657.                     }        
  658.                 }
  659.                 else
  660.                 {
  661.                     document.getElementById("useragentswitcher-move-down-button").setAttribute("disabled", true);
  662.                 }
  663.             }    
  664.         }
  665.         else
  666.         {
  667.             // If the delete button is disabled
  668.             if(document.getElementById("useragentswitcher-delete-button").hasAttribute("disabled"))
  669.             {
  670.                 document.getElementById("useragentswitcher-delete-button").removeAttribute("disabled");
  671.             }        
  672.             
  673.             document.getElementById("useragentswitcher-edit-button").setAttribute("disabled", true);
  674.             document.getElementById("useragentswitcher-move-down-button").setAttribute("disabled", true);
  675.             document.getElementById("useragentswitcher-move-up-button").setAttribute("disabled", true);
  676.         }
  677.     },
  678.     
  679.     // Handles double clicking on the tree
  680.     treeDoubleClick: function(event)
  681.     {
  682.         var tree        = document.getElementById("useragentswitcher-options-tree");
  683.         var rowPosition = tree.treeBoxObject.getRowAt(event.clientX, event.clientY);
  684.         var treeItem    = tree.view.getItemAtIndex(rowPosition);
  685.         
  686.         // If the tree item is set
  687.         if(treeItem)
  688.         {
  689.             // If the tree item is not a folder
  690.             if(!treeItem.hasAttribute("container"))
  691.             {
  692.                 UserAgentSwitcherOptions.editUserAgent(treeItem.firstChild.firstChild);
  693.             }
  694.         }
  695.     },
  696.     
  697.     // Uninitializes the options
  698.     uninitialize: function()
  699.     {
  700.         document.getElementById("useragentswitcher-options-user-agents").removeEventListener("dblclick", UserAgentSwitcherOptions.treeDoubleClick, false);
  701.     },
  702.     
  703.     // Updates the context menu
  704.     updateContextMenu: function()
  705.     {
  706.         var treeView   = document.getElementById("useragentswitcher-options-tree").view;
  707.         var selections = treeView.selection.getRangeCount();
  708.  
  709.         // If the clipboard is empty
  710.         if(this.clipboard.length == 0)
  711.         {
  712.             document.getElementById("useragentswitcher-paste").setAttribute("disabled", true);    
  713.         }
  714.  
  715.         // If there are no selections
  716.         if(selections == 0)
  717.         {
  718.             document.getElementById("useragentswitcher-copy").setAttribute("disabled", true);
  719.             document.getElementById("useragentswitcher-cut").setAttribute("disabled", true);
  720.             document.getElementById("useragentswitcher-delete-menu").setAttribute("disabled", true);
  721.             document.getElementById("useragentswitcher-edit-menu").setAttribute("disabled", true);
  722.             document.getElementById("useragentswitcher-move-up").setAttribute("disabled", true);
  723.             document.getElementById("useragentswitcher-move-down").setAttribute("disabled", true);
  724.             
  725.             // If the clipboard is not empty and the paste menu is disabled
  726.             if(this.clipboard.length > 0 && document.getElementById("useragentswitcher-paste").hasAttribute("disabled"))
  727.             {
  728.                 document.getElementById("useragentswitcher-paste").removeAttribute("disabled");
  729.             }        
  730.         }
  731.         else if(selections == 1)
  732.         {
  733.             var endIndex   = {};
  734.             var startIndex = {};
  735.             
  736.             treeView.selection.getRangeAt(0, startIndex, endIndex);
  737.             
  738.             // If more than one item is selected
  739.             if(endIndex.value - startIndex.value > 0)
  740.             {
  741.                 // If the copy menu is disabled
  742.                 if(document.getElementById("useragentswitcher-copy").hasAttribute("disabled"))
  743.                 {
  744.                     document.getElementById("useragentswitcher-copy").removeAttribute("disabled");
  745.                 }        
  746.                 
  747.                 // If the cut menu is disabled
  748.                 if(document.getElementById("useragentswitcher-cut").hasAttribute("disabled"))
  749.                 {
  750.                     document.getElementById("useragentswitcher-cut").removeAttribute("disabled");
  751.                 }        
  752.                 
  753.                 // If the delete menu is disabled
  754.                 if(document.getElementById("useragentswitcher-delete-menu").hasAttribute("disabled"))
  755.                 {
  756.                     document.getElementById("useragentswitcher-delete-menu").removeAttribute("disabled");
  757.                 }        
  758.                 
  759.                 document.getElementById("useragentswitcher-edit-menu").setAttribute("disabled", true);
  760.                 document.getElementById("useragentswitcher-move-up").setAttribute("disabled", true);
  761.                 document.getElementById("useragentswitcher-move-down").setAttribute("disabled", true);
  762.                 document.getElementById("useragentswitcher-paste").setAttribute("disabled", true);
  763.             }
  764.             else
  765.             {
  766.                 var selectedItem = treeView.getItemAtIndex(startIndex.value);
  767.             
  768.                 // If the copy menu is disabled
  769.                 if(document.getElementById("useragentswitcher-copy").hasAttribute("disabled"))
  770.                 {
  771.                     document.getElementById("useragentswitcher-copy").removeAttribute("disabled");
  772.                 }        
  773.                 
  774.                 // If the cut menu is disabled
  775.                 if(document.getElementById("useragentswitcher-cut").hasAttribute("disabled"))
  776.                 {
  777.                     document.getElementById("useragentswitcher-cut").removeAttribute("disabled");
  778.                 }        
  779.                 
  780.                 // If the delete menu is disabled
  781.                 if(document.getElementById("useragentswitcher-delete-menu").hasAttribute("disabled"))
  782.                 {
  783.                     document.getElementById("useragentswitcher-delete-menu").removeAttribute("disabled");
  784.                 }        
  785.                 
  786.                 // If a separator is selected
  787.                 if(this.isSeparatorSelected(selectedItem))
  788.                 {
  789.                     document.getElementById("useragentswitcher-edit-menu").setAttribute("disabled", true);
  790.                 }        
  791.                 else if(document.getElementById("useragentswitcher-edit-menu").hasAttribute("disabled"))
  792.                 {
  793.                     document.getElementById("useragentswitcher-edit-menu").removeAttribute("disabled");
  794.                 }        
  795.             
  796.                 // If the clipboard is not empty and the paste menu is disabled
  797.                 if(this.clipboard.length > 0 && document.getElementById("useragentswitcher-paste").hasAttribute("disabled"))
  798.                 {
  799.                     document.getElementById("useragentswitcher-paste").removeAttribute("disabled");
  800.                 }        
  801.                 
  802.                 // If the selected item is set and has a previous sibling
  803.                 if(selectedItem && selectedItem.previousSibling)
  804.                 {
  805.                     // If the move up menu is disabled
  806.                     if(document.getElementById("useragentswitcher-move-up").hasAttribute("disabled"))
  807.                     {
  808.                         document.getElementById("useragentswitcher-move-up").removeAttribute("disabled");
  809.                     }        
  810.                 }
  811.                 else
  812.                 {
  813.                     document.getElementById("useragentswitcher-move-up").setAttribute("disabled", true);
  814.                 }
  815.                 
  816.                 // If the selected item is set and has a next sibling
  817.                 if(selectedItem && selectedItem.nextSibling)
  818.                 {
  819.                     // If the move down menu is disabled
  820.                     if(document.getElementById("useragentswitcher-move-down").hasAttribute("disabled"))
  821.                     {
  822.                         document.getElementById("useragentswitcher-move-down").removeAttribute("disabled");
  823.                     }        
  824.                 }
  825.                 else
  826.                 {
  827.                     document.getElementById("useragentswitcher-move-down").setAttribute("disabled", true);
  828.                 }
  829.             }    
  830.         }
  831.         else
  832.         {
  833.             // If the copy menu is disabled
  834.             if(document.getElementById("useragentswitcher-copy").hasAttribute("disabled"))
  835.             {
  836.                 document.getElementById("useragentswitcher-copy").removeAttribute("disabled");
  837.             }        
  838.             
  839.             // If the cut menu is disabled
  840.             if(document.getElementById("useragentswitcher-cut").hasAttribute("disabled"))
  841.             {
  842.                 document.getElementById("useragentswitcher-cut").removeAttribute("disabled");
  843.             }        
  844.             
  845.             // If the delete menu is disabled
  846.             if(document.getElementById("useragentswitcher-delete-menu").hasAttribute("disabled"))
  847.             {
  848.                 document.getElementById("useragentswitcher-delete-menu").removeAttribute("disabled");
  849.             }        
  850.             
  851.             document.getElementById("useragentswitcher-edit-menu").setAttribute("disabled", true);
  852.             document.getElementById("useragentswitcher-move-up").setAttribute("disabled", true);
  853.             document.getElementById("useragentswitcher-move-down").setAttribute("disabled", true);
  854.             document.getElementById("useragentswitcher-paste").setAttribute("disabled", true);
  855.         }
  856.     }
  857. };
  858.